//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.ligson.springbootrestdao.event;

import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.internal.SessionFactoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManagerFactory;

@Component
public class HibernateListenerConfigurer {
    @Autowired
    private EntityManagerFactory emf;
    @Autowired
    private ApplicationContext applicationContext;

    public HibernateListenerConfigurer() {
    }

    @PostConstruct
    protected void init() {
        SessionFactoryImpl sessionFactory = (SessionFactoryImpl) this.emf.unwrap(SessionFactoryImpl.class);
        EventListenerRegistry registry = (EventListenerRegistry) sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
        CruxEventListener eventListener = new CruxEventListener(this.applicationContext);
//        registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener(eventListener);
//        registry.getEventListenerGroup(EventType.POST_UPDATE).appendListener(eventListener);
//        registry.getEventListenerGroup(EventType.POST_INSERT).appendListener(eventListener);
        registry.getEventListenerGroup(EventType.PRE_INSERT).appendListener(eventListener);
        registry.getEventListenerGroup(EventType.PRE_COLLECTION_RECREATE).appendListener(eventListener);
        registry.getEventListenerGroup(EventType.POST_COLLECTION_RECREATE).appendListener(eventListener);

//        registry.getEventListenerGroup(EventType.POST_DELETE).appendListener(eventListener);
//        registry.getEventListenerGroup(EventType.PRE_DELETE).appendListener(eventListener);
    }
}
